﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using InformationSystem.BLL.Core.Model;
using InformationSystem.WebSite.Utility;

namespace InformationSystem.WebSite
{
    public abstract class BasePage : System.Web.UI.Page
    {
        protected override void OnLoad(EventArgs e)
        {

            if (Session["LoginUser"] == null)
                Response.Redirect("~/Login.aspx");
            LoginUser = (LoginUser)Session["LoginUser"];
            SysEnvironment sev = null;
            if (Application[Common.AppRegInfo] != null)
            {
                sev = (SysEnvironment)Application[Common.AppRegInfo];
            }
            if (sev == null || sev.EndTime < DateTime.Now)
            {
                //无授权，或超期
                if (LoginUser.LoginUserType != UserType.Admin)
                {
                    Response.Redirect("~/404.aspx?errcode=noregisted", true);
                }
                else
                {
                    string rewuri = Request.RawUrl.ToLower();
                    if (!(rewuri.Contains("panel.aspx") && rewuri.Contains("m=reg")))
                        Response.Redirect("~/panel.aspx?m=reg", true);
                }
            }
            Application.Lock();
            List<LoginUser> useronlinelist = (List<LoginUser>)Application[Common.OnLine];
            LoginUser ui = useronlinelist.Find(val => val.UserID == LoginUser.UserID);
            if (ui != null)
            {
                ui.LastRefreshTime = DateTime.Now;
            }
            Application.UnLock();
            base.OnLoad(e);
        }
        protected LoginUser LoginUser { get; private set; }
        protected abstract bool CommitPermissionCheck();

        protected int OnLineUserCount
        {
            get
            {
                return OnLineHandler.GetAllOnlineUserCount((List<LoginUser>)Application[Common.OnLine], (int)Application[Common.TimeOut]);
            }
        }

        protected int OnLineGradeUserCount
        {
            get
            {
                return OnLineHandler.GetGradeAllOnlineUserCount((List<LoginUser>)Application[Common.OnLine], (int)Application[Common.TimeOut], LoginUser.CollegeID, LoginUser.GradeID);
            }
        }

        protected int OnLineGradeStudentCount
        {
            get
            {
                return OnLineHandler.GetGradeAllOnlineStudentCount((List<LoginUser>)Application[Common.OnLine], (int)Application[Common.TimeOut], LoginUser.CollegeID, LoginUser.GradeID);
            }
        }

        protected int OnLineGradeParentCount
        {
            get
            {
                return OnLineHandler.GetGradeAllOnlineParentCount((List<LoginUser>)Application[Common.OnLine], (int)Application[Common.TimeOut], LoginUser.CollegeID, LoginUser.GradeID);
            }
        }

        protected string OnLineGradeAssistantUser
        {
            get
            {
                return OnLineHandler.GetGradeAllOnlineAssistant((List<LoginUser>)Application[Common.OnLine], (int)Application[Common.TimeOut], LoginUser.CollegeID, LoginUser.GradeID);
            }
        }

    }
}